package com.upcode.weighing.admin.controller.sys;

import com.upcode.weighing.base.controller.BaseController;
import com.upcode.weighing.domain.SysRole;
import com.upcode.weighing.log.OpsLog;
import com.upcode.weighing.service.SysRoleResService;
import com.upcode.weighing.service.SysRoleService;
import com.upcode.weighing.service.SysUserRoleService;
import com.upcode.weighing.vo.add.SysRoleAddVo;
import com.upcode.weighing.vo.del.BaseDelVo;
import com.upcode.weighing.vo.edit.SysRoleEditVo;
import com.upcode.weighing.vo.req.PageReqVo;
import com.upcode.weighing.vo.req.pc.SysRoleListVo;
import com.upcode.weighing.vo.resp.PageRespVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "/role")
@Api(value = "/role",description = "角色接口",produces = MediaType.ALL_VALUE,tags = {"资源接口"})
public class RoleController extends BaseController {

    private static final Logger logger= LoggerFactory.getLogger(RoleController.class);

    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysUserRoleService sysUserRoleService;
    @Autowired
    private SysRoleResService sysRoleResService;


    @OpsLog(operation = "角色接口/新增角色/新增" ,type = 2)
    @ApiOperation(value = "新增角色", notes = "")
    @PostMapping("/add")
    public Object add(@Validated @RequestBody SysRoleAddVo addVo) {
        if(sysRoleService.checkRoleCode(addVo.getName(),addVo.getCode()).size()>0){
            return error("该角色已存在");
        }
        int i = sysRoleService.addRole(addVo);
        if (i>0) {
            return success("新增角色成功");
        }
        return error("新增角色失败");
    }

    @OpsLog(operation = "角色接口/修改角色/修改" ,type = 4)
    @ApiOperation(value = "修改角色", notes = "")
    @PostMapping("/edit")
    public Object edit(@Validated @RequestBody SysRoleEditVo editVo) {
        int i=sysRoleService.updateRole(editVo);
        if (i>0) {
            return success("修改角色成功");
        }
        return error("修改角色失败");
    }

    @OpsLog(operation = "角色接口/删除角色/删除" ,type = 3)
    @ApiOperation(value = "删除角色", notes = "")
    @PostMapping("/del")
    public Object del(@RequestBody BaseDelVo vo) {
        //1.删除角色与用户之间的绑定
        sysUserRoleService.delRoleUser(vo);
        //2.删除角色
        int i=sysRoleService.delRole(vo);
        if(i>0){
            return success("删除成功");
        }
        return error("删除角色失败");
    }

    @OpsLog(operation = "角色接口/角色分页列表/查询" ,type = 1)
    @ApiOperation(value = "角色分页列表", notes = "")
    @PostMapping(value = "/list")
    public Object list(@Validated @RequestBody PageReqVo<SysRoleListVo> vo) {
        PageRespVo<SysRole> page = sysRoleService.list(vo);
        if(page == null) {
            return error();
        }
        return success(page);
    }

    @OpsLog(operation = "角色接口/角色列表/查询" ,type = 1)
    @ApiOperation(value = "角色列表", notes = "")
    @PostMapping(value = "/listAll")
    public Object list() {
        List<SysRole> list = sysRoleService.roleList();
        return success(list);
    }

    @OpsLog(operation = "角色接口/角色详情/查询" ,type = 1)
    @ApiOperation(value = "角色详情", notes = "")
    @GetMapping(value = "/details/{id}")
    public Object details(@PathVariable("id") @NotNull(message = "角色ID不能为空") Long id) {
        SysRole sysRole = sysRoleService.getRoleById(id);
        return success(sysRole);
    }

    @OpsLog(operation = "角色接口/根据角色ID获取资源ID列表/查询" ,type = 1)
    @ApiOperation(value = "根据角色ID获取资源ID列表",notes ="")
    @GetMapping(value = "/getResIdByRoleId")
    public Object getResIdByRoleId(Long rId){
        if(sysRoleResService.getResIdByRoleId(rId)==null){
            return success(new ArrayList<>());
        }
        return success(sysRoleResService.getResIdByRoleId(rId));
    }
}
